﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.App.Input.Bean;
using Shpm.App.Input.Dao;
using Shpm.App.Master.Bean;
using Shpm.App.Master.Dao;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.System;
using Shpm.App.Commons.Utils;
using System.Reflection;
using Shpm.System.Web.Control;
using log4net;


public partial class App_Input_MenuForm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.ViewState["TYPE_FLG"] = false;
            //this.Context.Items["MENU_ID"] = 1;
            initDic();
            if (this.Context.Items["MENU_ID"] != null)
            {
                this.ViewState["MENU_ID"] = this.Context.Items["MENU_ID"];


            }
            else
            {
                this.ViewState["MENU_ID"] = null;
                Rbtn1.Checked = true;
                DrpWeek.Enabled = true;
            }
        }
        if (Rbtn1.Checked)
        {
            DrpWeek.Enabled = true;
            txtDate.Enabled = false;
        }
        else
        {
            DrpWeek.Enabled = false;
            txtDate.Enabled = true;
        }
    }
    //选择框数据绑定
    private void initDic()
    {

        CommonDictionaryBean paramBean = new CommonDictionaryBean();
        CommonDictionaryDao dao = new CommonDictionaryDao();
        /*用餐时间*/
        paramBean.Catalog = "M_DISH.DISH_TIME";
        initAll(dao, paramBean, this.DrpTime);
    }
    private void initAll(CommonDictionaryDao dao, CommonDictionaryBean paramBean, ListControl obj)
    {
        IList list = null;
        list = dao.FindByCatalog(paramBean);

        obj.DataSource = list;
        obj.DataBind();
    }
    void Page_PreRender(object sender, EventArgs e)
    {
        if (this.Context.Items["MENU_ID"] != null)
        {
            loadDate();
            if (Convert.ToBoolean(this.ViewState["TYPE_FLG"]))
            {
                //特殊
                Rbtn1.Visible = false;
                DrpWeek.Visible = false;
                Rbtn2.Visible = true;
                txtDate.Visible = true;
                txtDate.Enabled = false;
                DrpTime.Enabled = false;
            }
            else
            {
                //普通
                Rbtn1.Visible = true;
                DrpWeek.Visible = true;
                DrpWeek.Enabled = false;
                Rbtn2.Visible = false;
                txtDate.Visible = false;
                DrpTime.Enabled = false;
            }
        }
    }

    private void loadDate()
    {
        MenuBean dataBean = new MenuBean();
        MenuDetailBean mdBean = new MenuDetailBean();
        MenuDao Dao = new MenuDao();
        dataBean.MenuId = Convert.ToInt32(this.ViewState["MENU_ID"]);
        mdBean.MenuId = Convert.ToInt32(this.ViewState["MENU_ID"]);

        IList dataList = Dao.ListMenuDetail(mdBean);
        if (dataList.Count > 0)
        {
            DishList.Items.Clear();
            IEnumerator enm = dataList.GetEnumerator();
            while (enm.MoveNext())
            {
                mdBean = enm.Current as MenuDetailBean;
                ListItem itm = null;
                itm = new ListItem(mdBean.DishName, Convert.ToString(mdBean.DishId));
                DishList.Items.Add(itm);
            }
        }

        dataBean = Dao.GetMenu(dataBean);
        if (dataBean != null)
        {
            DrpTime.SelectedValue = dataBean.MenuTime;
            DrpWeek.SelectedValue = dataBean.MenuWeek;
            if (dataBean.MenuDate != null)
            {
                txtDate.Text = Convert.ToDateTime(dataBean.MenuDate).ToShortDateString();
            }

            if (dataBean.MenuWeek != null)
            {
                this.ViewState["TYPE_FLG"] = false;
                Rbtn1.Checked = true;
            }
            else
            {
                this.ViewState["TYPE_FLG"] = true;
                Rbtn2.Checked = true;
            }
        }
    }
    protected void save_Click(object sender, EventArgs e)
    {
        if (Rbtn2.Checked)
        {
            if (this.ComValidator1.DoValidator() == false)
            {

                ((Design_MasterPage)this.Master).InfoMessage = this.ComValidator1.ErrorMessage;

                return;
            }
        }
        string[] dishs = this.hidDishs.Value.Split(',');
        string[] dishId = new string[dishs.Length / 2];
        this.DishList.Items.Clear();
        if (dishs.Length > 1)
        {
            ListItem item = null;

            for (int i = 0; i < dishs.Length / 2; i++)
            {
                item = new ListItem();
                item.Text = dishs[i * 2 + 1];
                item.Value = dishs[i * 2];
                dishId[i] = dishs[i * 2];
                this.DishList.Items.Add(item);
            }
        }
        saveDate();
    }

    private void saveDate()
    {
        MenuDao Dao = new MenuDao();

        SysUserManager manager = new SysUserManager(Context);
        string userId = manager.getSysUserBean().getMstSysUserBean().SysUserId;


        //更新
        if (this.ViewState["MENU_ID"] != null)
        {
            saveMenuDetail(Convert.ToInt32(this.ViewState["MENU_ID"]), true);
        }
        //新建
        else
        {
            //判断数据是否存在
            MenuBean dataBean = new MenuBean();
            dataBean.MenuTime = DrpTime.SelectedValue;
            if (Rbtn1.Checked) //普通
            {
                dataBean.MenuWeek = DrpWeek.SelectedValue;
                dataBean.MenuDate = null;
            }
            else //特殊
            {
                dataBean.MenuWeek = string.Empty;
                dataBean.MenuDate = Convert.ToDateTime(txtDate.Text);
            }
            dataBean = Dao.CheakMenu(dataBean);
            if (dataBean != null)
            {
                ((Design_MasterPage)this.Master).InfoMessage =
                          HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0047").ToString();
                return;
            }

            //插入数据

            MenuBean muBean = new MenuBean();
            muBean.MenuTime = DrpTime.SelectedValue;
            if (Rbtn1.Checked) //普通
            {
                muBean.MenuWeek = DrpWeek.SelectedValue;
                muBean.MenuDate = null;
            }
            else //特殊
            {
                muBean.MenuWeek = string.Empty;
                muBean.MenuDate = Convert.ToDateTime(txtDate.Text);
            }
            int iMenuId = (int)Dao.InsertMenu(muBean);
            saveMenuDetail(iMenuId, false);
        }
    }

    private void saveMenuDetail(int Id,bool Flg)
    {
        IList lst = new ArrayList();
        MenuDao Dao = new MenuDao();
        SysUserManager manager = new SysUserManager(Context);
        string userId = manager.getSysUserBean().getMstSysUserBean().SysUserId;


        for (int i = 0; i < DishList.Items.Count; i++)
        {

            MenuDetailBean mdBean = new MenuDetailBean();
            mdBean.MenuId = Id;
            mdBean.DishId = Convert.ToInt32(DishList.Items[i].Value);
            mdBean.UserId = userId;
            lst.Add(mdBean);
        }

        if (Dao.InsertMenuDetailLst(lst,Id) != 0)
        {
            if (Flg)
            {
                ((Design_MasterPage)this.Master).InfoMessage =
                          HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0002").ToString();
            }
            else
            {
                ((Design_MasterPage)this.Master).InfoMessage =
                          HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0001").ToString();
            }
        }
    }
}
